package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.util.OCRUtil;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * @Author CuiZibo
 * @DATE 2018/12/18 15:14
 * @Description:
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping("/test")
    public String test() {
        logger.info("test_info()->{}","这里是log4j2");
        logger.error("test_error()->{}","这里是log4j2");
        logger.warn("test_warn()->{}","这里是log4j2");
        return "test";
    }

    @RequestMapping("/testResult")
    public Object testResult() {
        Result result = Result.NEW();
        result.setCode(-98);
        result.setData(new ArrayList<>());
        return result;
    }

    @RequestMapping("/testFilter")
    public String testFilter(@RequestParam(required = true) String v1,
                             @RequestParam String v2) {
        System.out.println(v1);
        System.out.println(v2);
        return v1;
    }

    @RequestMapping("/testMongo")
    public Object testMongo(@RequestParam(required = true) Long v1) {

        Query q = Query.query(Criteria.where("projectId").is(v1));
        EnterPersonCountByLaborService map = mongoTemplate.findOne(q, EnterPersonCountByLaborService.class);
        int i = 0;
        if (map != null) {
            Map<String, Map> data = map.getData();
            for (Map.Entry<String, Map> entry : data.entrySet()) {
                i += (int) entry.getValue().get("count");
            }
        }
        return i;
    }

    @RequestMapping("/ocrIdCard")
    public Object ocrIdCard(@RequestParam String base64File) {
        Result result = OCRUtil.ocrIdCard(base64File);
        return result;

    }

    @RequestMapping("/optimizeIdCard")
    public Object optimizeIdCard(@RequestParam String base64File) {
        Result result = OCRUtil.optimizeIdCard(base64File);
        return result;

    }
}
